﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChatDAL
{
    public class ChatManagement
    {

        public static User FindUserByID(int id)
        {
            User user = new User();
            ChatDBEntities entity = new ChatDBEntities();
            using (entity)
            {
                user = entity.Users.FirstOrDefault(u => u.UserID == id);
            }

            return user;
        }

        public static User FindUserByUserName(string userName)
        {
            User user = new User();
            ChatDBEntities entity = new ChatDBEntities();
            using (entity)
            {
                user = entity.Users.FirstOrDefault(u => u.UserName == userName);
            }

            return user;
        }

        public static List<User> GetAllUsers()
        {
            List<User> users = new List<User>();
            ChatDBEntities entity = new ChatDBEntities();
            using (entity)
            {
                users = entity.Users.ToList();
            }

            return users;
        }

        public static bool CheckUser(string userName, string password)
        {
            User user = new User();
            ChatDBEntities entity = new ChatDBEntities();
            using (entity)
            {
                user = entity.Users.FirstOrDefault(u => u.UserName == userName);
            }
            if (user.Password==password)
            {
                return true;
            }
            return false;
        }

        public static List<Channel> GetAllChannels()
        {
            List<Channel> channels = new List<Channel>();
            ChatDBEntities entity = new ChatDBEntities();
            using (entity)
            {
                channels = entity.Channels.ToList();
            }

            return channels;
        }

        public static List<Message> GetChannelMessages(int channelID)
        {
            List<Message> allMessages = new List<Message>();
            ChatDBEntities entity = new ChatDBEntities();
            using (entity)
            {
                allMessages = entity.Messages.Where(m => m.ChannelID == channelID).ToList();
            }
            List<Message> messages = new List<Message>();
            for (int i = allMessages.Count - 1; (i > messages.Count - 50) && i > -1; i--)
            {
                messages.Add(allMessages[i]);
            }

            return messages;
        }

        public static List<Message> GetPrivateMessages(int senderID, int recieverID)
        {
            List<Message> allMessages = new List<Message>();
            ChatDBEntities entity = new ChatDBEntities();
            using (entity)
            {
                allMessages = entity.Messages.Where(m => (m.SenderID == senderID)&&(m.RecieverID==recieverID)).ToList();
            }
            List<Message> messages = new List<Message>();
            for (int i = allMessages.Count - 1; (i > messages.Count - 20) && i > -1; i--)
            {
                messages.Add(allMessages[i]);
            }

            return messages;
        }

        public static void SendMessageToChannel(string messageText, int senderID, int channelID)
        {
            Message message = new Message()
            {
                Text = messageText,
                MessageTime = DateTime.Now,
                SenderID = senderID,
                ChannelID = channelID
            };

            ChatDBEntities entity = new ChatDBEntities();
            using (entity)
            {
                entity.Messages.AddObject(message);
                entity.SaveChanges();
            }
        }

        public static void SendMessageToUser(string messageText, int senderID, int recieverID)
        {
            Message message = new Message()
            {
                Text = messageText,
                MessageTime = DateTime.Now,
                SenderID = senderID,
                RecieverID = recieverID
            };

            ChatDBEntities entity = new ChatDBEntities();
            using (entity)
            {
                entity.Messages.AddObject(message);
                entity.SaveChanges();
            }
        }

        public static void CreateUser(User user)
        {
            ChatDBEntities entity = new ChatDBEntities();
            using (entity)
            {
                entity.Users.AddObject(user);
                entity.SaveChanges();
            }
        }

        public static void EditUser(int userID, User newUser)
        {
            ChatDBEntities entity = new ChatDBEntities();
            using (entity)
            {
                User userToEdit = entity.Users.FirstOrDefault(u => u.UserID == userID);

                if (userToEdit != null)
                {
                    entity.Attach(userToEdit);
                    userToEdit.UserName = newUser.UserName;
                    userToEdit.Password = newUser.Password;
                    userToEdit.FirstName = newUser.FirstName;
                    userToEdit.LastName = newUser.LastName;
                    userToEdit.Administrator = newUser.Administrator;
                }

                entity.SaveChanges();
            }
        }

        public static void DeleteUser(int userID)
        {
            ChatDBEntities entity = new ChatDBEntities();
            using (entity)
            {
                User userToDelete = entity.Users.FirstOrDefault(u => u.UserID == userID);

                if (userToDelete != null)
                {
                    entity.Attach(userToDelete);
                    entity.Users.DeleteObject(userToDelete);
                }

                entity.SaveChanges();
            }
        }

        public static void CreateChannel(Channel channel)
        {
            ChatDBEntities entity = new ChatDBEntities();
            using (entity)
            {
                entity.Channels.AddObject(channel);
                entity.SaveChanges();
            }
        }

        public static void EditChannel(int channelID, Channel newChannel)
        {
            ChatDBEntities entity = new ChatDBEntities();
            using (entity)
            {
                Channel channelToEdit = entity.Channels.FirstOrDefault(c => c.ChannelID == channelID);

                if (channelToEdit != null)
                {
                    entity.Attach(channelToEdit);
                    channelToEdit.ChannelName = newChannel.ChannelName;
                    channelToEdit.Description = newChannel.Description;
                }

                entity.SaveChanges();
            }
        }

        public static void DeleteChannel(int channelID)
        {
            ChatDBEntities entity = new ChatDBEntities();
            using (entity)
            {
                Channel channelToDelete = entity.Channels.FirstOrDefault(c => c.ChannelID == channelID);

                if (channelToDelete != null)
                {
                    entity.Attach(channelToDelete);
                    entity.Channels.DeleteObject(channelToDelete);
                }

                entity.SaveChanges();
            }
        }

        public static void CreateMessage(Message message)
        {
            ChatDBEntities entity = new ChatDBEntities();
            using (entity)
            {
                entity.Messages.AddObject(message);
                entity.SaveChanges();
            }
        }

        public static void EditMessage(int messageID, Message newMessage)
        {
            ChatDBEntities entity = new ChatDBEntities();
            using (entity)
            {
                Message messageToEdit = entity.Messages.FirstOrDefault(m => m.MessageID == messageID);

                if (messageToEdit != null)
                {
                    entity.Attach(messageToEdit);
                    messageToEdit = newMessage;
                }

                entity.SaveChanges();
            }
        }

        public static void DeleteMessage(int messageID)
        {
            ChatDBEntities entity = new ChatDBEntities();
            using (entity)
            {
                Message messageToDelete = entity.Messages.FirstOrDefault(m => m.MessageID == messageID);

                if (messageToDelete != null)
                {
                    entity.Attach(messageToDelete);
                    entity.Messages.DeleteObject(messageToDelete);
                }

                entity.SaveChanges();
            }
        }

        public static List<Message> GetAllMessages()
        {
            List<Message> allMessages = new List<Message>();
            ChatDBEntities entity = new ChatDBEntities();
            using (entity)
            {
                allMessages = entity.Messages.ToList();
            }

            return allMessages;
        }

        public static void RemoveUserMessages(int id)
        {
            ChatDBEntities entity = new ChatDBEntities();
            using (entity)
            {
                List<Message> messages = new List<Message>();
                messages = entity.Messages.Where(m => m.RecieverID == id).ToList();
                foreach (var item in messages)
                {
                    entity.Messages.DeleteObject(item);
                }
                messages = entity.Messages.Where(m => m.SenderID == id).ToList();
                foreach (var item in messages)
                {
                    entity.Attach(item);
                    item.SenderID = null;
                    entity.SaveChanges();
                }
            }
        }
    }
}
